package com.example.huadi1.service.Imp;

import com.example.huadi1.dto.BuyBook;
import com.example.huadi1.dto.Demand;
import com.example.huadi1.dto.Supplier;
import com.example.huadi1.dto.Textbook;
import com.example.huadi1.mapper.*;
import com.example.huadi1.service.BuyBookService;
import com.example.huadi1.service.StudentGetBookService;
import com.example.huadi1.service.TeacherGetBookService;
import com.example.huadi1.utils.AjaxResult;
import com.example.huadi1.utils.PageUtils;
import com.example.huadi1.utils.Request.PageRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

@Service
public class BuyBookServiceImp implements BuyBookService {
    @Autowired
    DemandMapper demandMapper;
    @Autowired
    BuyBookMapper buyBookMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    TextbookMapper textbookMapper;
    @Autowired
    TeacherGetBookMapper teacherGetBookMapper;
    @Autowired
    TeacherGetBookService teacherGetBookService;
    @Autowired
    StudentGetBookService studentGetBookService;
    /*

    */
    public AjaxResult createOneBuyBook(Demand demand){
        List<Supplier> suppliers = supplierMapper.selectSupplierByTextbook(demand.getTextbookId());
        Textbook tmp=textbookMapper.findById(demand.getTextbookId());
        Integer primary_num=Math.max(0,demand.getDemandNum()-tmp.getStock());
        suppliers.sort(Comparator.comparing(Supplier::getCost));
        for (Supplier supplier : suppliers) {
            if(primary_num<=0){
                break;
            }
            int buy_num=Math.min(supplier.getSupplyNum(),primary_num);
            BuyBook buyBook = new BuyBook(demand.getTextbookId(), supplier.getSupplierId(), buy_num, 0, buy_num * supplier.getCost());
            buyBookMapper.insertBuyBook(buyBook);
            primary_num=Math.max(0,primary_num-buy_num);
        }
        return AjaxResult.success();
    }
    public AjaxResult createBuyBook(){
        List<Demand> demands=demandMapper.selectAllPassDemand();
        for(int i=0;i<demands.size();i++){
            createOneBuyBook(demands.get(i));
        }
        return AjaxResult.success();
    }

    public AjaxResult buyTextbook(){
        List<BuyBook> buyBooks = buyBookMapper.selectAllBuyBookUnPass();
        for(BuyBook buyBook : buyBooks){
            textbookMapper.addStock(buyBook.getTextbookId(),buyBook.getBuyNum());
        }
        buyBookMapper.confirmAllBuyBookUnPass();
        teacherGetBookService.GenerateAllTeacherGetBook();
        studentGetBookService.GenerateAllStuGetBook();
        return AjaxResult.success();
    }

    public AjaxResult queryBuyBook(PageRequest pageRequest){
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<BuyBook> buyBooks = buyBookMapper.selectAllBuyBook();
        PageInfo<BuyBook> pageAll = PageInfo.of(buyBooks);
        PageUtils pageUtils = new PageUtils(pageAll);
        AjaxResult result=AjaxResult.success(pageUtils);
        return result;
    }
}
